package com.chenshu.shop.config;

import com.chenshu.shop.common.Constant;
import com.chenshu.shop.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA
 * Description：登陆拦截器（校验session）
 * User：chenshu
 * Date：2024-06-04
 * Time：18:01
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        //获取请求的URI
        String uri = request.getRequestURI();
        //用户信息验证
        if (uri.startsWith("/emall/index/")) {
            if (session != null && session.getAttribute(Constant.SESSION_USER_INFO_KEY) != null) {
                return true;
            }
            response.sendRedirect("/emall/index/login.jsp");
            return false;
        }
        //管理员信息验证
        if (uri.startsWith("/emall/admin/")) {
            if (session != null && session.getAttribute(Constant.SESSION_ADMIN_INFO_KEY) != null) {
                return true;
            }
            response.sendRedirect("/emall/admin/login.jsp");
            return false;
        }
        return true;
    }
}
